// pages/publish/examineTask.js
const httpUtils = require('../../utils/httpUtils')
const ui = require('../../utils/ui')
var currentIndex = 0;
Page({
  data: {
    examinedaishenhe: 0,
    examineyitongguo: 0,
    examineyijujue: 0,
    cellsOne: [],
    cellsTwo: [],
    cellsThree: [],
    page: 1,
    activeTab: 0,
    task_no: '',
  },
  onLoad(options) {
    this.setData({
      task_no: options.taskNo
    });
    this.requestList();
  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh() {
    this.setData({
      page: 1
    });
    this.requestList();
    wx.stopPullDownRefresh();
  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom() {
    this.setData({
      page: this.data.page + 1
    });
    this.requestList();
  },
//标签切换
onChange(event) {
  currentIndex = event.detail.index;
  this.setData({
    page: 1,
    activeTab: parseInt(event.detail.index)
  });
  this.requestList();
},
//点击图片预览
previewImage(event){
  var currentUrl = event.currentTarget.dataset.src //获取当前点击图片链接
  var urls = event.currentTarget.dataset.urls //获取当前图片列表
  wx.previewImage({
    current:currentUrl,//当前点击的预览图片地址
    urls: urls,//需要预览的图片列表，这是必须设置的属性
  })
},
//拒绝任务
taskRejuctRequest(event) {
  var that = this;
  wx.showModal({
    title: '提示',
    confirmText: '拒绝',
    editable: true,
    placeholderText: '请输入原因',
    complete: (res) => {
      console.log(res.content);
      if (res.content.length == 0) {
        ui.showToast('请输入拒绝理由');
        return;
      } else {
        if (res.confirm) {
          let obj = {
            method: 'PUT',
            contentType: 'application/x-www-form-urlencoded',
            url: '/task-management/task-order/' + event.currentTarget.dataset.orderno ,
            data: {
              orderNo: event.currentTarget.dataset.orderno,
              verifyStatus: '拒绝',
              comment: res.content
            }
          }
          httpUtils.request(obj).then(res => {
            wx.showToast({
              title: '已拒绝',
              duration: 2000,
              mask: true,
              success: function() {
                setTimeout(function() {
                  that.requestList();
                }, 1500)
              }
            });
          }).catch(err => {
            console.log('ERROR')
          });
        }
      }
    }
  });
},
//通过任务
taskPassRequest(event) {
  var that = this;
  wx.showModal({
    title: '提示',
    confirmText: '通过',
    content: '通过后，奖赏将自动转到对方账户内。',
    complete: (res) => {
      if (res.confirm) {
        let obj = {
          method: 'PUT',
          contentType: 'application/x-www-form-urlencoded',
          url: '/task-management/task-order/' + event.currentTarget.dataset.orderno ,
          data: {
            orderNo: event.currentTarget.dataset.orderno,
            verifyStatus: '通过',
            comment: res.content
          }
        }
        httpUtils.request(obj).then(res => {
          wx.showToast({
            title: '已通过',
            duration: 2000,
            mask: true,
            success: function() {
              setTimeout(function() {
                that.requestList();
              }, 1500)
            }
          });
        }).catch(err => {
          console.log('ERROR')
        });
      }
    }
  });
},
//加入黑名单
actionBlackRequest(event) {
  wx.showModal({
    title: '提示',
    content: '是否要将对方拉入黑名单',
    complete: (res) => {
      if (res.confirm) {
        let obj = {
          method: 'POST',
          contentType: 'application/x-www-form-urlencoded',
          url: '/user/black-list/' + event.currentTarget.dataset.platformid,
          data: {}
        }
        httpUtils.request(obj).then(res => {
          ui.showToast("已加入黑名单");
        }).catch(err => {
          console.log('ERROR')
        });
      }
    }
  });
},
//请求列表
requestList() {
  var typeStr = "待审核";
  if (currentIndex == 1) {
    typeStr = "已通过";
  } else if (currentIndex == 2) {
    typeStr = "已拒绝";
  } 
  let obj = {
    method: 'GET',
    url: '/task/published/'+this.data.task_no+'/orders',
    showLoading: true,
    data: {
      page: this.data.page,
      size: 20,
      orderStatus: typeStr
    }
  }
  httpUtils.request(obj).then(res => {
    if (currentIndex == 2) {
      if (this.data.page == 1) {
        this.setData({
          cellsThree: []
        });
      }
      const {
        cellsThree = []
      } = this.data;
      res.data.taskOrderSumitInfoDtos.forEach(function (item, index) {
        cellsThree.push(item);
      });
      this.setData({
        cellsThree,
        examineyijujue: res.data.count
      });
    } else if (currentIndex == 1) {
      if (this.data.page == 1) {
        this.setData({
          cellsTwo: []
        });
      }
      const {
        cellsTwo = []
      } = this.data;
      res.data.taskOrderSumitInfoDtos.forEach(function (item, index) {
        cellsTwo.push(item);
      });
      this.setData({
        cellsTwo,
        examineyitongguo: res.data.count
      });
    } else {
      if (this.data.page == 1) {
        this.setData({
          cellsOne: []
        });
      }
      const {
        cellsOne = []
      } = this.data;
      res.data.taskOrderSumitInfoDtos.forEach(function (item, index) {
        cellsOne.push(item);
      });
      this.setData({
        cellsOne,
        examinedaishenhe: res.data.count
      });
    }
    
  }).catch(err => {
    console.log(err)
  });
}
})